<?php
/**
*+-------用户信息中心操作类------------------------+
*+-------------------------------------------------+
*+----用户基础信息，用户点赞，关注，订阅操作-------+
*+-------------------------------------------------+
*+---点赞是type|1对视频，type|2对赛事--------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/

namespace Home\Controller;
use Think\Page;

class UcenterController extends BaseUserController {

	/**
	 * 个人资料设置的项限制
	 */
	protected $profileField = array(
		'game', 'desc', 'gender', 'age', 'address', 'stars', 'interest'
	);


	protected function _initialize(){

		parent::_initialize();
		C('DEFAULT_THEME', 'ui3');

		$eventObj = A('Api/User', 'Event');
		$this->user = $eventObj->detail(UID, true);

		// if(ACTION_NAME == 'attention')
		// 	$this->noself = 1;

		// $this->hasBGD = 1;//页面加背景图

	}


	/**
	*用户好友动态数
	*/
	private function hasTrade(){

		$tradeNum = 0;

		if($this->user['statis']['follows'] || $this->user['statis']['scribe']){
			$tradeNum = 1;
		}else{

			$vEvent = A('Api/Video', 'Event');
			$recommend = $vEvent->getRecommend();
			$tradeNum = count($recommend);
		}
		
		$this->assign('tradeNum', $tradeNum);	
	}

	/**
	*用户新消息数
	*/
	private function newMsg(){

		$msgEvent = A('Api/Message', 'Event');
		$newMsgNum = $msgEvent->getNewCount(UID);
		$this->assign('newMsgNum', $newMsgNum);
	}

	/**
	*用户首页
	*/
	public function index(){
		return A('Visitor')->index();
		$this->commentPager = 5;
		$this->adCode = 'webhome';
		if(IS_AJAX){
			$res = $this->_activity(I('timestamp',0,'intval'));
			$this->ajaxReturn(array('state'=>0, 'info'=>'操作成功', 'data'=>$res));
		}else{
			$this->hotVideo = A('Home/Visitor')->dailyRecommend();
			$this->tradeList = $this->_activity();
			//header tab
			$this->assign('currentTab', 'home');
			$this->assign('title', '66Play');
			$this->display();
		}
	}

	/**
	*获取好友动态列表
	*/
	private function _activity($timestamp = 0) {
		$fEvent = A('Api/Follow', 'Event');
		$comEvent  = A('Api/Comments', 'Event');
		$scribeEvent = A('Api/GameScribe', 'Event');
		$servVideo = D('Common/Video', 'Service');

		$friends = $fEvent->mine(UID);
		$scribes = $scribeEvent->mine(UID);
		$listRow = 10;

		$query[] = array('recommend'=>$servVideo::RECOMMEND_YES);
		$friends && $query[]['uid'] = array('$in' => $friends );
		$scribes && $query[]['game_id'] =  array('$in' => $scribes );
		$map['$and'][]['$or'] = $query;
		$map['$and'][]['create_time'] = $timestamp ? array('$lt' => $timestamp) : array('$gt' => 0);
		$map['$and'][]['status'] = $servVideo::STATUS_PASS;
		$map['$and'][]['race_id'] = ['$eq' => null];

		$where['_array'] = $map;
		$res = $servVideo->where($where)->order('create_time desc')->limit($listRow)->field('id')->select();
		foreach ($res as $key => $value) {
			$videos[] = $value['id'];
		}

		$filterMap = array('id','title','thumb','pre_uri','uri', 'dpi_levels', 'detail','user','statis','action','create_time','game_id','game','type','icons', 'tags', 'type', 'status', 'dpi_height', 'dpi_width');
		$videos = $this->lists_format($videos, $filterMap);

		if($videos){
			$count = count($videos);
            $this->timestamp = $videos[$count-1]['timestamp'];

			$comments = array();
			foreach ($videos as $key => &$video) {
				$video['comments'] = $comEvent->get($video['id'], $this->commentPager, '', array(), true);
				$video['comments_show_count'] = count($video['comments']);
			}
		}
		return $videos;
	}

	/**
	*与我相关
	*即好友信息，以时间戳进行分页, 倒序进行
	*
	*/
	public function message(){
		$this->title = '与我相关';
		C('DEFAULT_THEME', 'v4');
		return $this->display();

		$messEvent = A('Api/Message', 'Event');
		$likeEvent = A('Api/Like', 'Event');
		$comEvent  = A('Api/Comments', 'Event');
		$vEvent    = A('Api/Video', 'Event');
		$gEvent    = A('Api/Game', 'Event');

		$map = array();
		$map['to'] = UID;
		$timestamp = I('timestamp',0, 'intval');
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);

		$pager 	   = C('MESSAGE_CENTER_NUM',NULL, 2);
		$resData = $messEvent->getMsgList($map, $pager);
		
		if($resData){

			foreach ($resData['ids'] as $key => $videoId) {
				$video = $vEvent->detail($videoId, UID, true);//modify by susy.liang 直接取游戏信息 2015/7/2
				$video['create_time'] = unixtime_format($video['create_time']);

				field_filter($video['game'], array('id', 'name', 'pic'));//add by susy.liang 过滤参数 2015/7/2
				/*
				*modify by zhaov
				*/
				/*if(1 === intval($video['type'])){
					$game = $gEvent->info($video['game_id']);
					field_filter($game, array('id', 'name', 'pic'));
					uri_format($game['pic']);
					$video['game'] = $game;
				}*/
				//end

				$data['video']    = $video;
				
				if($video['uid'] == UID){
					$data['zan']      = $likeEvent->getUser($videoId,5);
					$data['zan']      = $data['zan'] ? $data['zan'] : array();

					$data['comments'] = $comEvent->get($videoId, 5, '', array(), true, false);
					$data['comments'] = $data['comments'] ? $data['comments'] : array();
				}else{

					$query = array();
					$cmtMap = array();
					$where = array();

					$query[]['uid']     = array('$eq'=>intval(UID));
					$query[]['referrer'] =  array('$eq'=>intval(UID));
					$cmtMap['$and'][]['$or'] = $query;
					$cmtMap['$and'][]['video_id'] = intval($videoId);
					$where['_array'] = $cmtMap;
					
					$data['comments'] = $comEvent->get(0, '', '', $where, true);

				}				
				$list[] = $data;
			}

			$this->timestamp = $resData['timestamp'];
		}

		$this->data = $list;
		$this->msgHtml = $this->fetch('Template:message');

		$msgMap = array('target_id'=>array('in',$resData['ids']));
		$messEvent->setRead('', $msgMap);

		if(IS_AJAX){
			$this->ajaxReturn(array('state'=>0, 'info'=>'获取成功', 
				'data'=>array('total'=>count($resData['ids']), 'limit'=>$pager, 'content'=>$this->msgHtml)));
		}else{
			//$this->hotHtml = $this->getHot();
			$this->hotVideo = A('Home/Video')->recommend();
			$this->display();
		}						
	}

	/**
	*账号设置
	*/
	public function setting(){

		//$this->hotHtml = $this->getHot();
		$this->hotVideo = A('Home/Video')->recommend();

		// // $data = A('Api/Feedback', 'Event')->info(UID);
		// if(null == $data){
		// 	$this->feedback = '欢迎广大用户提出你们的宝贵意见，让我们为大家提供更好的服务。';
		// }else{
		// 	$this->feedback = $data['content'];
		// }

		$this->display();
	}

	/**
	*用户关注和订阅信息
	*type 0|粉丝 1|关注 2|游戏
	*/
	public function attention(){

		$this->type = $type = I('type', 0, 'intval');

		$p = I('p', 0, 'intval');

		//$this->hotHtml = $this->getHot();
		$this->hotVideo = A('Home/Video')->recommend();
		
		if($type == 2){//游戏
			$this->getGame($p);
		}else{//关注和粉丝情况
			$this->getFollow($type, $p);
		}

		$this->display();
	}

	/**
	*取游戏列表
	*/
	private function getGame($p=0){

		$gameObj = A('Api/Game', 'Event');
		$scribeObj = A('Api/GameScribe', 'Event');

		$total = $scribeObj->count_mine(UID);
		if(!$total)
			return false;

		$limitNum = C('GAMES_CENTER_NUM');
		$pageObj = new \Think\Page($total, $limitNum);
        if($total>$limitNum){
            $pageObj->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        }

        $page =$pageObj->show();
        $this->assign('page', $page? $page: '');
        $this->assign('total',$total);

		$games = $scribeObj->mine(UID, true, $p, $limitNum);
		foreach ($games as $key => &$game) {
			uri_format($game['cover']);
		}
		$this->data = $games;

		return true;
	}

	/**
	*获取用户关注的情况
	*@param $type 0|粉丝 1|我的关注
	*/
	private function getFollow($type=0, $p){

		$listNum = C('FANS_CENTER_NUM');

		$eventObj = A('Api/Follow', 'Event');

		if($type)//我关注的
			$total = $eventObj->count_mine(UID);
		else
			$total = $eventObj->count_follows(UID);

		if(!$total)
			return false;

		$limitNum = C('FANS_CENTER_NUM');
		$pageObj = new \Think\Page($total, $limitNum);
        if($total>$limitNum){

            $pageObj->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        }

        $page =$pageObj->show();
        $this->assign('page', $page? $page: '');
        $this->assign('total',$total);

        if($type)
        	$data = $eventObj->mine(UID, true, array(), $limitNum, $p);
        else
        	$data = $eventObj->follows(UID, true, array(), $limitNum, $p);

        $this->data = $data;
        return true;
	}

	/**
	*获取登录用户的头像，昵称信息，
	*对应ajax请求
	*/
	public function info(){

		$data = $this->uinfo;
		unset($data['id']);
		unset($data['username']);

		$this->ajaxReturn($this->uinfo);

	}

	
	/**
	 * 设置用户的资料
	 */
	public function setProfile() {
		
		$data = I('');

		unset($data['ajax']);
		foreach ( $data as $key => $value ) {
			if ( !in_array( $key, $this->profileField ) ) {
				$this->error('参数项有误');
			}
			else{
				$data[$key] = trim($value);
			}
		}

		$flg = A( 'Api/User', 'Event' )->setProfile( UID, $data );
		if ( $flg===false )
			$this->error('服务器出错' );
		else
			$this->success( '设置成功' );

	}

	/**
	 * 设置头像
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function setAvatar()
	{
		$data = I('');
		$avatar = I('avatar');

		$scale = I('scale', 1, 'floatval'); //比例
		$size = I('size', 100, 'intval'); //大小
		$ax   = I('ax', 0, 'intval');
		$ay   = I('ay', 0, 'intval');

		if (!$avatar || !$scale || !$size) {
			$this->ajaxReturn(['state' => -2, 'info' => '参数错误']);
		}
		$size = $size / $scale;
		$ax   = $ax / $scale;
		$ay   = $ay / $scale;

		$crop = [
			'iopcmd' => 'crop',
			'ax'     => intval($ax),
			'ay'     => intval($ay),
			'height' => intval($size),
			'width'  => intval($size)
		];
		$scale = floatval(100/$size);
		$thumb = [
			'iopcmd' => 'thumbnail',
			'type'   => 1,
			'scale'  => floatval(100/$size)*100
		];

		$avatar .= '?';
		$avatar .= http_build_query($crop);
		$avatar .= '|';
		$avatar .= http_build_query($thumb);

		$data = ['avatar' => $avatar];
		if (service('Users')->where(['id' => UID])->save($data)) {
			// 更改头像增加积分
			$score = event('Score')->get(UID, 'avatar');
			$score != false && $data['score_plus'] = $score;

			// 更新session中用户头像
			$session_user = session('home_user_auth');
	        $session_user['avatar'] = $avatar;
	        session('home_user_auth', $session_user);
	        session('home_user_auth_sign', data_auth_sign($session_user));

			$this->ajaxReturn(['state' => 0, 'data' => $data, 'info' => '头像保存成功']);
		} else {
			$this->ajaxReturn(['state' => -2, 'info' => '头像保存失败']);
		}
	}

	/**
	 * 设置背景
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function setBackground()
	{
		$data = I('');
		$background = I('background');
		$scale = I('scale', 1, 'floatval'); //比例
		$height = I('height', 300, 'intval'); //高
		$width = I('width', 180, 'intval'); //宽
		$ax   = I('ax', 0, 'intval');
		$ay   = I('ay', 0, 'intval');

		if (!$background || !$scale || !$height || !$width) {
			$this->ajaxReturn(['state' => -2, 'info' => '参数错误']);
		}
		$height = $height / $scale;
		$width = $width / $scale;
		$ax   = $ax / $scale;
		$ay   = $ay / $scale;

		$crop = [
			'iopcmd' => 'crop',
			'ax'     => intval($ax),
			'ay'     => intval($ay),
			'height' => intval($height),
			'width'  => intval($width)
		];
		$scale = floatval(300/$height);
		$thumb = [
			'iopcmd' => 'thumbnail',
			'type'   => 1,
			'scale'  => floatval(100/$height)*100
		];

		$background .= '?';
		$background .= http_build_query($crop);
		$background .= '|';
		$background .= http_build_query($thumb);

		$data = ['background' => $background];
		if (service('Users')->where(['id' => UID])->save($data)) {
			$score = event('Score')->get(UID, 'bg');
			$score != false && $data['score_plus'] = $score;
			$this->ajaxReturn(['state' => 0, 'data' => $data, 'info' => '背景保存成功']);
		} else {
			$this->ajaxReturn(['state' => -2, 'info' => '背景保存失败']);
		}
	}

	/**
	*用户个人资料
	*@author susy.liang<liangling@ilongyuan.com.cn>
	*/
	// 重写 liubo<liubo@ilongyuan.com.cn> 2016-8-30
	public function profile(){
		$this->title = '个人资料';

		$this->ages = range(10, 99);
		$this->stars = array(
						'白羊座','金牛座','双子座', '巨蟹座',
						'狮子座', '处女座', '天秤座', '天蝎座',
						'射手座', '魔羯座', '水瓶座', '双鱼座'
						);
		uri_format($this->user['background']);
		uri_format($this->user['avatar']);
		$this->games = implode(',', $this->user['game']);
		$this->interest = $this->user['interest'] ? explode(',', $this->user['interest']) : [];
		C('DEFAULT_THEME', 'v4');
		return $this->display();
	}

	public function historyVideo($offset = 0) {
		$type = 0;// 视频
		$total = event('history')->total( UID , ['type' => $type]);
		$details = [];
		$pager = I( 'pager', 12, 'intval' );
		if ( $total ) {
			$map = [
				'uid' => UID,
				'type' => $type
			];
			$list = event('history')->lists($map, $pager, $offset);
			$vids = $list ? array_column($list, 'video_id') : [];
			$videos = event('video')->getBaseInfoLists(['id' => ['in', $vids]], true);
			$videos = array_column($videos, null, 'id');
			foreach ($list as $value) {
				if (!empty($videos[$value['video_id']])) {
					$video = $videos[$value['video_id']];
				} else {
					$video = [];
				}
				$video['watch_date'] = date('Y-m-d', $value['create_time']);
				$video['watch_month'] = date('Y-m', $value['create_time']);
				$details[] = $video;
			}
		}
		C('DEFAULT_THEME', 'v4');
		if (IS_AJAX) {
			$data = [];
			foreach ($details as $video) {
				$this->video = $video;
				$data[] = [
					'watch_date' => $video['watch_date'],
					'watch_month' => $video['watch_month'],
					'html' => $this->fetch('Ucenter/historyVideoTemplate'),
				];
			}
			$this->ajaxReturn(['state' => 0, 'info' => '请求成功', 'data' => $data]);
		}
		$details = array_group($details, 'watch_month');
		$this->p = $offset/$pager;
		$this->pager = $pager;
		$this->total = $total;
		$this->histories = $details;
		$this->title = '查看记录';
		$this->display('Ucenter/historyvideo');
	}

	public function historyRace($offset = 0)
	{
		$type = 1; // 比赛
		$total = event('history')->total( UID , ['type' => $type]);
		$details = [];
		$pager = I( 'pager', 12, 'intval' );
		if ( $total ) {
			$map = [
				'uid' => UID,
				'type' => $type
			];
			$lists = event('history')->lists($map, $pager, $offset);
			$vids = array_column($lists, 'video_id');
			$videos = service('video')->where(['id' => ['in', $vids]])->select();
			$videos = array_column($videos, null, 'id');
			$fieldMap = ['id', 'title', 'subtitle', 'race_date', 'race_hour', 'score', 'team', 'game', 'match', 'cover', 'race_title'];
			foreach ($lists as $key => $value) {
				if (empty($videos[$value['video_id']]['group_id'])) {
					continue;
				}
				$race_id = $videos[$value['video_id']]['group_id'];
				$detail = event('RaceGroup')->info($race_id, UID);
				if (!$detail) {
					continue;
				}
				$detail['race_title'] = $videos[$value['video_id']]['title'];
				$detail['score'] = $detail['score']?array_map('intval', explode('-', $detail['score'])):[];
	            $detail['game'] = $this->field_filter($detail['game'], ['id','name','logo']);
	            $detail['match'] = $this->field_filter($detail['match'], ['id','title','cover', 'pc_cover']);
	            $detail = $this->field_filter($detail, $fieldMap);
	            uri_format($detail['cover']);
                uri_format($detail['match']['cover']);
                uri_format($detail['match']['pc_cover']);
                if (empty($detail['match']['pc_cover'])) {
                    $detail['match']['pc_cover'] = $detail['match']['cover'];
                }

                $detail['video_id'] = $value['video_id'];
				$detail['watch_month'] = date('Y-m', $value['create_time']);
				$detail['watch_date'] = date('Y-m-d', $value['create_time']);
				$details[] = $detail;
			}
		}
		C('DEFAULT_THEME', 'v4');
		if (IS_AJAX) {
			$data = [];
			foreach ($details as $race) {
				$this->race = $race;
				$data[] = [
					'watch_month' => $race['watch_month'],
					'watch_date' => $race['watch_date'],
					'html' => $this->fetch('Ucenter/historyRaceTemplate'),
				];
			}
			$this->ajaxReturn(['state' => 0, 'info' => '请求成功', 'data' => $data]);
		}
		$details = array_group($details, 'watch_month');
		$this->p = $offset/$pager;
		$this->pager = $pager;
		$this->total = $total;
		$this->histories = $details;
		$this->title = '查看记录';
		$this->display('Ucenter/historyrace');
	}

	/**
	 * 用户的收藏列表信息
	 *@author susy.liang<liangling@ilongyuan.com.cn>
	 */
	public function favour() {
		return $this->favourVideo();

		$fEvent = A( 'Api/Favour', 'Event' );

		$type=I( 'get.type', 'match', 'strip_tags' );
		$this->type = $type;
		
		$type = $type=='match'?1:0;
		$total = $fEvent->uTotal( UID, $type );

		if ( $total ) {

			$p = I( 'p', 1, 'intval' );
			$pager = I( 'pager', C( 'HOME_VIDEO_PER', NULL, 10 ), 'intval' );
			$lists = $fEvent->get( UID, $p, $pager, $type );

			$details = array();
			$vEvent = A('Api/Video', 'Event');
			foreach ($lists as $key => $value) {
				$video = $vEvent->detail($value['video_id'], UID);
				if($video){
					$video['favour_time'] = unixtime_format($value['create_time']);
					$details[] = $video;
				}
			}

			$this->p = $p+1;//下一页的页码传给js加载用
		}

		$this->data = $details;

		if(IS_AJAX){
			if($total)
				$strhtml = $this->fetch('Template:myvideo');

			$this->ajaxReturn(array('state'=>0, 'info'=>'操作成功', 
							'data'=>array('total'=>count($lists), 'limit'=>$pager, 'content'=>$strhtml))
							);
		}else{

			$this->hotVideo = A('Home/Video')->recommend();			
			$this->display();
		}
	}

	/**
	 * 视频收藏
	 * @author liubo<liubo@ilongyuan.com.cn> 2016-11-15
	 * @param  integer $offset [description]
	 */
	public function favourVideo($offset = 0)
	{
		$type = 0; // 视频
		$total = event('Favour')->uTotal(UID, $type);
		$details = [];
		$pager = I( 'pager', 12, 'intval' );
		if ( $total ) {
			$map = [
				'uid' => UID,
				'type' => $type
			];
			$lists = event('favour')->lists($map, $pager, $offset);
			foreach ($lists as $key => $value) {
				$video = event('Video')->detail($value['video_id'], UID);
				if($video){
					$video['favour_month'] = date('Y-m', $value['create_time']);
					$video['favour_date'] = date('Y-m-d', $value['create_time']);
					$details[] = $video;
				}
			}
		}
		C('DEFAULT_THEME', 'v4');
		if (IS_AJAX) {
			$data = [];
			foreach ($details as $video) {
				$this->video = $video;
				$data[] = [
					'favour_month' => $video['favour_month'],
					'favour_date' => $video['favour_date'],
					'html' => $this->fetch('Ucenter/favourVideoTemplate'),
				];
			}
			$this->ajaxReturn(['state' => 0, 'info' => '请求成功', 'data' => $data]);
		}
		$details = array_group($details, 'favour_month');
		$this->p = $offset/$pager;
		$this->pager = $pager;
		$this->total = $total;
		$this->favours = $details;
		$this->title = '我的收藏';
		$this->display('Ucenter/favourVideo');
	}

	/**
	 * 比赛收藏
	 * @author liubo<liubo@ilongyuan.com.cn> 2016-11-15
	 * @param  integer $offset [description]
	 */
	public function favourRace($offset = 0)
	{
		$type = 1; // 比赛
		$total = event('Favour')->uTotal(UID, $type);
		$details = [];
		$pager = I( 'pager', 12, 'intval' );
		if ( $total ) {
			$map = [
				'uid' => UID,
				'type' => $type
			];
			$lists = event('favour')->lists($map, $pager, $offset);
			$vids = array_column($lists, 'video_id');
			$videos = service('video')->where(['id' => ['in', $vids]])->select();
			$videos = array_column($videos, null, 'id');
			$fieldMap = ['id', 'title', 'subtitle', 'race_date', 'race_hour', 'score', 'team', 'game', 'match', 'cover', 'race_title'];
			foreach ($lists as $key => $value) {
				if (empty($videos[$value['video_id']]['group_id'])) {
					continue;
				}
				$race_id = $videos[$value['video_id']]['group_id'];
				$detail = event('RaceGroup')->info($race_id, UID);
				if (!$detail) {
					continue;
				}
				$detail['race_title'] = $videos[$value['video_id']]['title'];
				$detail['score'] = $detail['score']?array_map('intval', explode('-', $detail['score'])):[];
	            $detail['game'] = $this->field_filter($detail['game'], ['id','name','logo']);
	            $detail['match'] = $this->field_filter($detail['match'], ['id','title','cover', 'pc_cover']);
	            $detail = $this->field_filter($detail, $fieldMap);
	            uri_format($detail['cover']);
                uri_format($detail['match']['cover']);
                uri_format($detail['match']['pc_cover']);
                if (empty($detail['match']['pc_cover'])) {
                    $detail['match']['pc_cover'] = $detail['match']['cover'];
                }

                $detail['video_id'] = $value['video_id'];
				$detail['favour_month'] = date('Y-m', $value['create_time']);
				$detail['favour_date'] = date('Y-m-d', $value['create_time']);
				$details[] = $detail;
			}
		}
		C('DEFAULT_THEME', 'v4');
		if (IS_AJAX) {
			$data = [];
			foreach ($details as $race) {
				$this->race = $race;
				$data[] = [
					'favour_month' => $race['favour_month'],
					'favour_date' => $race['favour_date'],
					'html' => $this->fetch('Ucenter/favourRaceTemplate'),
				];
			}
			$this->ajaxReturn(['state' => 0, 'info' => '请求成功', 'data' => $data]);
		}
		$details = array_group($details, 'favour_month');
		$this->p = $offset/$pager;
		$this->pager = $pager;
		$this->total = $total;
		$this->favours = $details;
		$this->title = '我的收藏';
		$this->display('Ucenter/favourRace');
	}

	/**
	 * 关注的游戏页面
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function games() {
		C('DEFAULT_THEME', 'v4');
		$res = [];
		$gameEvent = event('Game');
		$gameScribeEvent = event('GameScribe');
		$games = $gameEvent->lists(null, 'id');
		foreach ($games as $value) {
			$game = $gameEvent->info($value['id']);
			$game['is_scribe'] = $gameScribeEvent->is_scribe(UID, $value['id']);
			uri_format($game['cover']);
			$res[] = $game;
		}
		$this->gameList = $res;

		$this->assign('currentTab', 'ucenter');
		$this->assign('title', '关注游戏');
		$this->display();
	}

	/**
	 * 个人关注页面
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function follows() {
		$this->page = $page = I('p', 1, 'intval');
		$nickname = I('nickname', '', 'trim');
		$followEvent = event('Follow');

		$this->pager = 10;
		$this->follows_list = $followEvent->getFollows(UID, true, [], false, $this->pager, $page, $nickname);
		C('DEFAULT_THEME', 'v4');
		if (IS_AJAX) {
			$html = '';
			foreach ($this->follows_list as $follow) {
				$this->follow = $follow;
				$html .= $this->fetch('Ucenter/followsTemplate');
			}
			$this->ajaxReturn(['state' => 0, 'data' => ['html' => $html]]);
		}

		$this->followsCount = $followEvent->getFollowsCount(UID, [], false, $nickname);
		$this->nickname = $nickname;
		$this->assign('currentTab', 'ucenter');
		$this->assign('title', '我的关注');
		$this->display();
	}

	/**
	 * 个人粉丝页面
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function fans() {
		$this->page = $page = I('p', 1, 'intval');
		$nickname = I('nickname', '', 'trim');
		$followEvent = event('Follow');

		$this->pager = 10;
		$this->fans_list = $followEvent->getFollows(UID, true, [], true, $this->pager, $page, $nickname);
		C('DEFAULT_THEME', 'v4');
		if (IS_AJAX) {
			$html = '';
			foreach ($this->fans_list as $fans) {
				$this->fans = $fans;
				$html .= $this->fetch('Ucenter/fansTemplate');
			}
			$this->ajaxReturn(['state' => 0, 'data' => ['html' => $html]]);
		}

		$this->fansCount = $followEvent->getFollowsCount(UID, [], true, $nickname);
		$this->nickname = $nickname;
		$this->assign('title', '我的粉丝');
		$this->display();
	}

	/**
	 * 用户个人动态
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function activity() {
		$timestamp = I('timestamp', 0, 'intval');
		$pager = I('pager', 10, 'intval');
		$this->commentPager = 5;
		$data = $this->_myActivity($timestamp, $pager);
		if(IS_AJAX){
			$this->ajaxReturn(array('state'=>0, 'info'=>'操作成功', 'data'=>$data));
		}else{
			$this->hotVideo = A('Home/Visitor')->dailyRecommend();

			$this->tradeList = $data;

			$this->assign('title', '我的动态');
			$this->display('Ucenter/activity');
		}
	}

	/**
	 * 账号设置
	 * @author liubo <liubo@ilongyuan.com.cn>
	 * @return [type] [description]
	 */
	public function account()
	{
		$this->title = '账号安全';

		C('DEFAULT_THEME', 'v4');
		$this->display();
	}

	/**
	 * 我的任务
	 * @author liubo <liubo@ilongyuan.com.cn>
	 * @return [type] [description]
	 */
	public function mission()
	{
		$this->title = '我的任务';

		C('DEFAULT_THEME', 'v4');
		return $this->display();
	}

	/**
	 * 修改密码
	 * @author liubo<liubo@ilongyuan.com.cn> 2016-11-16
	 */
	public function setPassword()
	{
		if (IS_POST) {
			$data = I('');
			$res = event('User')->resetPwd(UID, $data);
			if (!empty($res['code']) && $res['code'] == 200) {
				D('Member')->logout();
			}
			$this->ajaxReturn($res);
		}
		$this->title = '修改密码';
		C('DEFAULT_THEME', 'v4');
		return $this->display('setPassword');
	}

	private function _myActivity($timestamp, $pager) {
		$videoEvent = event('video');
		$map['uid'] = UID;

		$this->totalCount = $videoEvent->total($map);
		$map['create_time'] = $timestamp ? array('$lt' => $timestamp) : array('$gt' => 0);
		$videos = $videoEvent->lists($map, $pager);

		$filterMap = array('id','title','thumb','pre_uri','uri', 'dpi_levels', 'detail','user','statis','action','create_time','game_id','game','type','icons', 'tags', 'type', 'status', 'dpi_width', 'dpi_height');
		$videos = $this->lists_format($videos, $filterMap);

		if($videos){
			$count = count($videos);
            $this->timestamp = $videos[$count-1]['timestamp'];

			$comments = array();
			$comEvent = event('comments');
			foreach ($videos as $key => &$video) {
				$video['comments'] = $comEvent->get($video['id'], $this->commentPager, '', array(), true);
				$video['comments'] = array_map(function($comment){
                    $comment['content'] = htmlspecialchars($comment['content']);
                    return $comment;
                }, $video['comments']);
				$video['comments_show_count'] = count($video['comments']);
			}
		}
		return $videos;
	}
}
